<?php


namespace app\common\traites;


trait GetTreeTrait
{
    public function fillModelBackends($models,$id='id',$pid='pid')
    {
        $list = [];
        foreach ($models as $model) {

            $listItem = $this->fillModelBackend($model,$id,$pid);
            $list[] = $listItem;
        }
        return $list;
    }

    public function fillModelBackend($model,$id,$pid)
    {
        if ($model[$id] ==0 )return $model;
        $loadList = $this->model->cache(60)->where([$pid => $model[$id]])->select()->toArray();
        if (count($loadList) > 0) {
            $model['children'] = $this->fillModelBackends($loadList,$id,$pid);
        }
        //else {
        //    $model['children'] = [];
        //}
        return $model;
    }
}